package com.avic.rong.payUtil;

import android.app.Activity;
import android.content.Context;

import com.avic.rong.R;
import com.avic.rong.payUtil.pay.WexinAliPayRsultCallBack;
import com.avic.rong.payUtil.pay.payAli.AliPay;
import com.avic.rong.payUtil.pay.payWeiXin.WeixinPay;
import com.avic.rong.utils.ToastTools;


/**
 * 专家咨询、音视频课程、专栏订阅、VIP套餐、付费圈子购买
 * created by xtj on 2019/2/15 9:27.
 */
public class JavaPayUtil {
    private Context context;
    private PaySuccessCallBack paySuccessCallBack;
    private String consultType;
    public JavaPayUtil(Context context){
        this.context = context;
    }
    public JavaPayUtil(Context context, PaySuccessCallBack callBack){
        this.context = context;
        this.paySuccessCallBack = callBack;
    }
    public JavaPayUtil(Context context, PaySuccessCallBack callBack, String consultType){
        this.context = context;
        this.paySuccessCallBack = callBack;
        this.consultType=consultType;
    }

    public interface PaySuccessCallBack{
        void paySuccess();
        void expertPaySuccess(String consultType);
        void payCancel();
    }
    /**
     * //支付方式，WeChat：微信支付；Alipay：支付
     * @param payVo
     * @param payWay
     */
    public void payByWay(PayVo payVo, String payWay){
        switch (payWay) {
            case "Alipay":// 支付宝支付类型
                aliPay(payVo);

                break;
            case "WeChat":// 微信支付类型--支付结果
                weixinPay(payVo);
                break;
        }

    }
    private void weixinPay(PayVo payVo){


        WeixinPay weixinPay = new WeixinPay(wexinAliPayRsultCallBack);
        weixinPay.sendPayReq(payVo, context);
    }
    private void aliPay(PayVo payVo){
        AliPay aliPay = new AliPay((Activity) context, wexinAliPayRsultCallBack);
        aliPay.pay(payVo.getPayParams());
    }
    WexinAliPayRsultCallBack wexinAliPayRsultCallBack = new WexinAliPayRsultCallBack() {
        @Override
        public void onSucceed() {
//            ToastTools.showToast(context,"恭喜您，支付成功");
            if (paySuccessCallBack!=null){
                paySuccessCallBack.paySuccess();
                paySuccessCallBack.expertPaySuccess(consultType);
            }
        }

        @Override
        public void onFailure() {
//            hidePayStateDialog();
            if (paySuccessCallBack!=null){
                paySuccessCallBack.payCancel();
            }
//            ToastTools.showToast(context, R.string.pay_failed);
        }

        @Override
        public void onCancel() {
//            hidePayStateDialog();
            if (paySuccessCallBack!=null){
                paySuccessCallBack.payCancel();
            }
//            ToastTools.showToast(context, R.string.pay_cancel);

        }
    };
}
